package com.poxz.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poxz.common.exception.BusinessException;
import com.poxz.common.response.ResultCode;
import com.poxz.mybatisplus.mapper.UserMapper;
import com.poxz.mybatisplus.model.dto.UserDTO;
import com.poxz.mybatisplus.model.entity.User;
import com.poxz.mybatisplus.model.vo.UserVO;
import com.poxz.mybatisplus.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务实现类
 *
 * @author poxz
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    private final UserMapper userMapper;

    @Override
    public UserVO getUserById(Long id) {
        User user = this.getById(id);

        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_FOUND);
        }

        return UserVO.from(user);
    }

    @Override
    public UserVO createUser(UserDTO userDTO) {
        // 检查用户名是否重复
        long count = this.count(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, userDTO.getUsername())
        );
        if (count > 0) {
            throw new BusinessException(ResultCode.FAIL, "用户名已存在");
        }

        // 默认性别为"未知"
        if (userDTO.getGender() == null) {
            userDTO.setGender(0);
        }

        User user = User.builder()
                .username(userDTO.getUsername())
                .nickname(userDTO.getNickname())
                .email(userDTO.getEmail())
                .phone(userDTO.getPhone())
                .birthday(userDTO.getBirthday())
                .gender(userDTO.getGender())
                .avatar(userDTO.getAvatar())
                .build();

        this.save(user);
        return UserVO.from(user);
    }

    @Override
    public UserVO updateUser(Long id, UserDTO userDTO) {
        User user = this.getById(id);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_FOUND);
        }

        User updatedUser = User.builder()
                .id(id)
                .username(userDTO.getUsername())
                .nickname(userDTO.getNickname())
                .email(userDTO.getEmail())
                .phone(userDTO.getPhone())
                .birthday(userDTO.getBirthday())
                .gender(userDTO.getGender())
                .avatar(userDTO.getAvatar())
                .build();

        this.updateById(updatedUser);
        return UserVO.from(updatedUser);
    }

    @Override
    public void deleteUser(Long id) {
        User user = this.getById(id);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_FOUND);
        }

        this.removeById(id);
    }
}




